<?php
/**
 * 分页类
 * Usag:
 * @package Bic
 * @Author:Zuolong Wang
 * @version 0.3
 */
if (! defined ( 'IN_BIC' )) {
    die ( '非法图像处理访问来源!' );
}
class Bic_View_Helper_Page extends Bic_Base{
    /**
     * 信息总数
     * @var int
     */
    private static $_total = null;
    /**
     * 分面总数
     * @var int
     */
    private static $_pages = null;
    private static $_pageName = null;
    /**
     * 当前页数
     * @var int
     */
    private static $_currentPage = null;
    /**
     * 每页几条
     * @var int
     */
    private static $_offset = null;
    /**
     * 是否开启ajax
     * @var BOOL
     */
    private static $_ajax = false;
    
    private static $_linkStyle = array();
    private static $_textStyle = array();
    
    private static $_view =null;
    private static $_framework =null;
    
    public $_firstText = '最前页';
    public $_lastText = '最后页';
    public $_prevText = '上一页';
    public $_nextText = '下一页';

    public function __construct($config){
        self::$_total = $config['total']>0?$config['total']:1;
        self::$_currentPage = $config['page']>0?$config['page']:1;
        self::$_offset = $config['offset'];
        self::$_ajax = (''!=$config['ajax'])?$config['ajax']:false;
        self::$_pageName = $config['page_name'];
        //页面总数
        self::$_pages = ceil(self::$_total/self::$_offset);
        
        self::$_view = $config['view']!=''?$config['view']:null;
        self::$_framework = $config['framework']!=''?$config['framework']:null;
        
        self::$_ajax = $config['ajax'];
    }

    /**
     * 下一页码
     * @param $page
     * @return int
     */
    public function nextPage($page=NULL){
        $page = is_null($page) ? self::$_currentPage : $page;
        
        if ($page < self::$_pages){
            return $this->getLink($page+1,$this->_nextText);
        }
        return $this->getText($page+1,$this->_nextText);
    }

    /**
     * 上一页码
     * @param $page
     * @return int
     */
    public function prevPage($page=NULL){
        $page = is_null($page) ? self::$_currentPage : $page;
        
        if ($page>1){
            return $this->getLink($page-1,$this->_prevText);
        }
        return $this->getText($page-1,$this->_prevText);
    }

    /**
     * 第一页码
     * @param $page
     * @return int
     */
    public function firstPage(){
        if (self::$_currentPage!=1){
            return $this->getLink(1,$this->_firstText);
        }
        return $this->getText(1,$this->_firstText);
    }

    /**
     * 最后一页码
     * @param $page
     * @return int
     */
    public function lastPage(){
        if (self::$_currentPage!=self::$_pages){
            return $this->getLink(self::$_pages,$this->_lastText);
        }
        return $this->getText(self::$_pages,$this->_lastText);
    }

    /**
     * 页码栏
     * @param $type
     * @return string
     */
    public function pageBar($type){
        switch ($type){
            case 1:
                for ( $i=self::$_pages ; $i >=1  ; $i--){
                    if ( $i != self::$_currentPage ){
                        $pageBar .= $this->getLink($i,$i);
                    }else{
                        $pageBar .= $this->getText($i,$i);
                    }
                }
                return $pageBar;
                break;
        }
    }

    /**
     * 创建下拉跳转元素
     * @return string
     */
    public function select(){}

    /**
     * 创建输入框跳转元素
     * @return string
     */
    public function input(){}

    /**
     * 获取分页链接文字
     * @param $page	链接
     * @return string
     */
    public function getLink($page,$text=null){
        $name = (''==$text)?$page:$text;
        if (self::$_currentPage == $page){
            $currentPageStyle = self::$_linkStyle['currentPageStyle'];
            $currentPageClass = self::$_linkStyle['currentPageClass'];
        }
        if (!empty(self::$_framework)){
            /**
             * 调用框架获取url链接
             * @var unknown_type
             */
            $href = $this->getFrameworkLink($page);
        }else{
            $href = '?'.$this -> getUrlParameter('page').'&'.self::$_pageName.'='.$page;;
        }
        return "<a class='".self::$_linkStyle['class']." {$currentPageClass}' style='".self::$_linkStyle['style']."{$currentPageStyle}' href='{$href}'>{$name}</a>";
    }

    /**
     * 获取分页文字
     * @param $page	文字
     * @return string
     */
    public function getText($page,$text=null){
        $name = (''==$text)?$page:$text;
        if (self::$_currentPage == $page){
            $currentPageStyle = self::$_linkStyle['currentPageStyle'];
            $currentPageClass = self::$_linkStyle['currentPageClass'];
        }
        switch (self::$_ajax){
            case false:
                return "<span class='".self::$_textStyle['class']." {$currentPageClass}' style='cursor:text;color:#666;text-decoration:none;".self::$_textStyle['style']."{$currentPageStyle}' href='javascript:void(0);'>{$name}</span>";
                break;
            case true:
            default:
                break;
        }
    }
    
    /**
     * 添加样式
     * @param $style
     * @return null
     */
    public function addStyle(){
        switch (func_num_args()){
            case 1:
                $style = func_get_arg(0);
                break;
            case 2:
                $style['type'] = func_get_arg(0);
                $style['style'] = func_get_arg(1);
                break;
        }
        switch ($style['type']){
            case 'link':
                self::$_linkStyle['style'] .= $style['style'];
                break;
            case 'text':
                self::$_textStyle['style'] .= $style['style'].' ';
                break;
        }
    }
    
    /**
     * 添加class
     * @param $style
     * @return null
     */
    public function addClass(){
        switch (func_num_args()){
            case 1:
                $style = func_get_arg(0);
                break;
            case 2:
                $style['type'] = func_get_arg(0);
                $style['class_name'] = func_get_arg(1);
                break;
        }
        switch ($style['type']){
            case 'link':
                self::$_linkStyle['class'] .=  $style['class_name'];
                break;
            case 'text':
                self::$_textStyle['class'] .=  $style['class_name'].' ';
                break;
        }
    }
    
    /**
     * 选中页面特别样式添加样式
     * @param $style
     * @return null
     */
    public function addCurrentPageStyle($style){
        self::$_linkStyle['currentPageStyle'] .= $style;
    }
    
    /**
     * 选中页面特别Class添加样式
     * @param $style
     * @return null
     */
    public function addCurrentPageClass($class){
        self::$_linkStyle['currentPageClass'] .= $class.' ';
    }
    
    protected function getFrameworkLink($page=1){
        switch (self::$_framework){
            case 'BicPHP':
            default:
                $action_param = Bic_Router::getParams();
                $action_param[self::$_pageName] = $page;
                if (self::$_ajax['ajax_open'] === true){
                    self::$_ajax['ajax_page_name'] = self::$_pageName;
                    $config = self::$_ajax;
                }
                return Bic_View::getInstance() -> url(Bic_Router::getCurrentController().'/'.Bic_Router::getCurrentAction(),$action_param,$config);
                break;
        }
    }
    
    /**
     *获取url参数
     *  带有过滤垃圾参数的功能
     *  usage:
     *  	当前页面地址为http://www.example.com/index.php?param1=1&param2=2
     *  	调用get_url_parameter()将返回:param1=1&param2=2
     * @param string $order 过滤选项，多个用","号分隔
     * @param string $arr 要处理的数组，默认为$_GET
     * @param string $arr_key 递归调用参数[不了解，最好默认！不填写！]
     * @return string
     */
    public function getUrlParameter($order,$arr='',$arr_key=NULL){
        $url='';
        $fields=explode(',',$order);
        $arr || $arr=$_GET;
        foreach ($arr as $get_key=>$get_val){
            /**
             * uri 里面含有数组
             * 	用了递归的处理方法
             */
            if(is_array($get_val)){
                $t = get_url_parameter('',$get_val,$get_key);

                if($url==''){
                    $url=$t;
                }else{
                    $url.="&{$t}";
                }
                continue;
            }

            if ($arr_key){
                /**
                 * 递归调用进入此处理
                 */
                if($url==''){
                    $url.=$arr_key."[$get_key]={$get_val}";
                }else{
                    $url.="&$arr_key"."[$get_key]={$get_val}";
                }

            }else{
                if(array_search($get_key,$fields)!==false){
                    continue;
                }

                if($url==''){
                    $url.="{$get_key}={$get_val}";
                }else{
                    $url.="&{$get_key}={$get_val}";
                }
            }
        }
        return $url;
    }

    public function addLinkClass(){}
    public function addTextClass(){}
    public function addSelectClass(){}
    public function addInputClass(){}
    public function addLinkStyle(){}
    public function addTextStyle(){}
    public function addSelectStyle(){}
    public function addInputStyle(){}
    
    public function debug(){}
    
    public function render($type){
        switch ($type){
            case 1:
                
                $this->addClass('link','pageLink');
                $this->addClass('text','pageText');
                $this->addCurrentPageClass('currentPage');
                
                
                $firstPageLink = $this->getLink(1);
                $totalLink = $this->getLink(self::$_pages);
                
                $pageInstance = "{$this->prevPage()}";
                    
                if (self::$_currentPage<5 && self::$_pages>5){
                    $maxNum = self::$_currentPage>3?self::$_currentPage+2:5;
                    for($i=1;$i<=$maxNum;$i++){
                        $page = $i!=self::$_currentPage?$this->getLink($i):$this->getText($i);
                        $pageInstance .= "{$page}";
                    }
                    $pageInstance .= "<span class='ellipsis '>...</span>{$totalLink}";
                }else if (self::$_pages>5){
                    $pageInstance .= "{$firstPageLink}<span class='ellipsis '>...</span>";
                    
                    $minNum = self::$_currentPage-2;
                    $maxNum = self::$_currentPage+2>self::$_pages?self::$_pages:self::$_currentPage+2;
                    for($i=$minNum;$i<=$maxNum;$i++){
                        $page = $i!=self::$_currentPage?$this->getLink($i):$this->getText($i);
                        $pageInstance .= "{$page}";
                    }
                    $pageInstance .= self::$_currentPage+2<self::$_pages?"<span class='ellipsis '>...</span>":'';
                    $pageInstance .= self::$_currentPage+2<self::$_pages?"{$totalLink}":'';
                }else{
                    for($i=1;$i<=self::$_pages;$i++){
                        $page = $i!=self::$_currentPage?$this->getLink($i):$this->getText($i);
                        $pageInstance .= "{$page}";
                    }
                }
                
                $pageInstance .= $this->nextPage();
                return $pageInstance;
                break;
            case 2:
                $this->_firstText = Model_Lang::import($_GET['lg'],'zuiqianye');
                $this->_prevText = Model_Lang::import($_GET['lg'],'shangyiye');
                $this->_nextText = Model_Lang::import($_GET['lg'],'xiayiye');
                $this->_lastText = Model_Lang::import($_GET['lg'],'zuihouye');
                return 
                Model_Lang::import($_GET['lg'],'di').self::$_currentPage."/".self::$_pages.Model_Lang::import($_GET['lg'],'ye')." ".Model_Lang::import($_GET['lg'],'gong')." ".self::$_total." ".Model_Lang::import($_GET['lg'],'tiaojilu')."
                                            ".$this->firstPage()."
                                            ".$this->prevPage()."
                                            ".$this->nextPage()."
                                            ".$this->lastPage()."
                ";
                break;
            case 3:
                $this->addClass('link','conr_pagesp');
                $this->addClass('text','conr_pagespon');
                $this->addStyle('text','color:#fff');
                return $this->pageBar(4);
                break;
            case 4:
                return 
                "第".self::$_currentPage."/".self::$_pages."页&nbsp;&nbsp;共".self::$_total."条记录
                &nbsp;&nbsp;".$this->firstPage()."
                &nbsp;&nbsp;".$this->prevPage()."
                &nbsp;&nbsp;".$this->nextPage()."
                &nbsp;&nbsp;".$this->lastPage()."
                ";
                break;
        }
    }
}

?>